import { login, getUserInfo, getUserDetail } from '@/api/user'

export default {
  namespaced: true,
  state: {
    token: null,
    userInfo: {},
    timeStamp: ''
  },
  mutations: {
    setToken(state, token) {
      console.log(token)
      state.token = token
      state.timeStamp = Date.now()
    },
    setUserInfo(state, payload) {
      state.userInfo = payload
    }
  },
  actions: {
    async loginAction(context, payload) {
      const res = await login(payload)
      context.commit('setToken', res)
    },
    async getUserInfo(context) {
      const res = await getUserInfo()

      const detail = await getUserDetail(res.userId)
      //  将两个接口返回的数据合并，一块存到 userInfo中
      const userInfo = { ...res, ...detail }
      // console.log(res)
      // context.commit('setUserInfo', 'userInfo')
      context.commit('setUserInfo', userInfo)
    },
    // 登出action，
    logout(context) {
      context.commit('setToken', null)
      context.commit('setUserInfo', {})
    }
  }
}
